#include "Customer.h"
#include <fstream>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <iomanip>
using namespace std;
// 客户类构造函数，初始化客户姓名、储蓄账户和支票账户
Customer::Customer(const string &customerName,
                   const shared_ptr<SavingsAccount> &savings,
                   const shared_ptr<CheckingAccount> &checking)
    : name(customerName), savingsAccount(savings), checkingAccount(checking)
{
}

// 获取客户姓名
string Customer::getName() const
{
    return name;
}

// 获取客户的储蓄账户
shared_ptr<SavingsAccount> Customer::getSavingsAccount() const
{
    return savingsAccount;
}

// 获取客户的支票账户
shared_ptr<CheckingAccount> Customer::getCheckingAccount() const
{
    return checkingAccount;
}

// 获取客户的所有交易记录
vector<shared_ptr<Transaction>> Customer::getAllTransactions() const
{
    vector<shared_ptr<Transaction>> allTransactions;

    // 添加储蓄账户的交易记录
    const auto &savingsTransactions = savingsAccount->getTransactions();
    allTransactions.insert(allTransactions.end(), savingsTransactions.begin(), savingsTransactions.end());

    // 添加支票账户的交易记录
    const auto &checkingTransactions = checkingAccount->getTransactions();
    allTransactions.insert(allTransactions.end(), checkingTransactions.begin(), checkingTransactions.end());

    // 按时间戳排序交易记录
    sort(allTransactions.begin(), allTransactions.end(),
              [](const shared_ptr<Transaction> &a, const shared_ptr<Transaction> &b)
              {
                  return a->getTimestamp() < b->getTimestamp();
              });

    return allTransactions;
}

// 生成客户报告并保存到文件
void Customer::generateCustomerReport(const string &filename) const
{
    // 打开输出文件流
    ofstream reportFile(filename);

    // 检查文件是否成功打开
    if (!reportFile.is_open())
    {
        cerr << "Error: Unable to create report file: " << filename << endl;
        return;
    }

    // 写入客户基本信息
    reportFile << "===== Customer Transactions Report =====" << endl;
    reportFile << "Customer: " << name << endl;
    reportFile << "Savings Account: " << savingsAccount->getAccountId() << endl;
    reportFile << "Savings Balance: ¥" << fixed << setprecision(2) << savingsAccount->getBalance() << endl;
    reportFile << "Checking Account: " << checkingAccount->getAccountId() << endl;
    reportFile << "Checking Balance: ¥" << fixed << setprecision(2) << checkingAccount->getBalance() << endl;
    reportFile << "Total Balance: ¥" << fixed << setprecision(2)
               << (savingsAccount->getBalance() + checkingAccount->getBalance()) << endl;
    reportFile << "==============================================" << endl
               << endl;

    // 获取所有交易记录
    vector<shared_ptr<Transaction>> allTransactions = getAllTransactions();

    // 检查是否有交易记录
    if (allTransactions.empty())
    {
        reportFile << "No transactions found for this customer." << endl;
    }
    else
    {
        // 写入交易历史记录标题
        reportFile << "Transaction History (chronological order):" << endl
                   << endl;

        // 遍历并输出每条交易记录
        for (const auto &transaction : allTransactions)
        {
            reportFile << "---------------------------------------------" << endl;
            reportFile << transaction->toString() << endl;
        }
    }

    // 关闭文件并显示成功信息
    reportFile.close();
    cout << "Report successfully generated as \"" << filename << "\"" << endl;
}